Public Class ForgotPassword
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents txtEmailAddr As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnRequest As System.Web.UI.WebControls.ImageButton
    Protected WithEvents lblErr As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            If Request("id") <> "" Then
                Me.ChangePassword()
            End If
        End If
    End Sub

    Protected Sub ChangePassword()
        Dim dt As DataTable
        Dim sMsg As New System.Text.StringBuilder

        Try
            dt = CUser.GetUserByChangePasswordRequestId(Request("id"))
        Catch ex As Exception
            sMsg.Append("Your change request is invalid, please review your URL or contact our administrator.")
            Business.Common.ShowMessageBoard(sMsg.ToString, "http://" + Business.Common.WebServerName(Request) + AppRoot() + "/Login.aspx")
        End Try

        ' Generate new password
        Randomize()
        Dim sPassword As String = ""
        Dim i As Integer
        For i = 0 To 7
            sPassword += CStr(CInt(Rnd() * 9))
        Next

        ' Save this password
        CUser.UpdatePassword(CInt(dt.Rows(0).Item(0)), HashPassword(sPassword))

        ' Send notification email to User
        Try
            Dim sWebserver As String = Business.Common.WebServerName(Request)
            Dim sAppRoot As String = AppRoot()

            Dim TemplateItems As New Hashtable
            TemplateItems.Add(MailService.NewPasswordTemplateItems.UserFullName, CStr(dt.Rows(0).Item(1)))
            TemplateItems.Add(MailService.NewPasswordTemplateItems.LoginName, CStr(dt.Rows(0).Item(2)))
            TemplateItems.Add(MailService.NewPasswordTemplateItems.NewPassword, sPassword)
            Dim path As String = Server.MapPath(Request.ApplicationPath)
            Dim mail As New MailService(MailService.eMailType.NewPasswordNotification, TemplateItems, path)

            mail.Send(CStr(dt.Rows(0).Item(4)))

            sMsg.Append("Your password has been generated successfully." + vbCrLf + "An email containing new password has been sent to you.")
        Catch ex As Exception
            sMsg.Append("Your password has been changed, but we could not send it to you." + vbCrLf + "Please contact our administrator.")
        End Try
        Business.Common.ShowMessageBoard(sMsg.ToString, "http://" + Business.Common.WebServerName(Request) + AppRoot() + "/Login.aspx")
    End Sub

    Private Overloads Sub btnRequest_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnRequest.Click
        Dim sGuid As String
        Dim sMsg As New System.Text.StringBuilder

        Try
            sGuid = CUser.GenerateChangePasswordGUID(txtEmailAddr.Text)
        Catch ex As Exception
            lblErr.Text = "This email address does not exist!"
            Return
        End Try

        'Send confirm email to this User
        Try
            Dim sEmail As String = txtEmailAddr.Text.Trim()
            Dim sWebserver As String = Business.Common.WebServerName(Request)
            Dim sAppRoot As String = AppRoot()

            Dim TemplateItems As New Hashtable
            Dim s As String
            s = String.Format("http://" + sWebserver + sAppRoot + "/ForgotPassword.aspx?id={0}", sGuid)
            's = Server.HtmlDecode(s)

            TemplateItems.Add(MailService.CustomerApprovalTemplateItems.ChangePasswordLink, s)
            Dim path As String = Server.MapPath(Request.ApplicationPath)
            Dim mail As New MailService(MailService.eMailType.CustomerApprovalNotification, TemplateItems, path)


            mail.Send(sEmail)

            sMsg.Append("An email has been sent to you." + vbCrLf + "Please follow instructions in the email to approve of providing your new password.")
        Catch ex As Exception
            sMsg.Append("We could not send email to you." + vbCrLf + "Please contact our administrator for more information.")
        End Try
        Business.Common.ShowMessageBoard(sMsg.ToString, "http://" + Business.Common.WebServerName(Request) + AppRoot() + "/Login.aspx")
    End Sub
End Class
